<?php
  $path = $data ? 'vat_tu/phieu_vat_tu/sua_phieu/bantp/'.$data['phieu']['ma_phieu'] : 'vat_tu/ban_thanh_pham/nhap_phieu';
  $class = 'nhap-phieu-ban-tp';
  $i = 0;
?>
<form method="post" action="<?php echo base_path() . $path ?>" id="nhap-phieu-ban-tp" name="nhap-phieu-ban-tp" class="<?php echo $class ?>">
  <div class="form-item form-select-wrapper to-san-xuat-wrapper">
    <label for="txt-to-san-xuat">Bộ phận</label>
    <input id="txt-to-san-xuat" value="<?php echo ($_POST && $_POST['to-san-xuat']) ? $_POST['to-san-xuat'] : ((isset($data['phieu'])) ? $data['phieu']['ref'].' ['.$data['phieu']['ma_ref'].']' : '') ?>"
           name="to-san-xuat" type="text"
           class="form-text form-autocomplete text txt-to-san-xuat <?php echo ($_POST && (string) $_POST['error']['key'] == 'to-san-xuat')
             ? 'error' : '' ?>" autocomplete="OFF"/>
    <input class="autocomplete" type="hidden" id="txt-to-san-xuat-autocomplete"
           value="<?php echo base_path() . 'to_san_xuat/autocomplete' ?>" disabled="disabled"/>

    <div class="clear"></div>
  </div>
  <div class="clear"></div>
  <fieldset>
    <legend>Nhập số lượng vật tư</legend>
    <div id="form-item-wrapper">
      <?php if ($_POST && isset($_POST['ten_vat_tu'])) { ?>
      <?php foreach ($_POST['ten_vat_tu'] as $key => $ten_vat_tu) { ?>
        <?php $i++; ?>
        <div class="form-item-row">
          <div class="form-item form-text-wrapper">
            <label for="txt-ten-vat-tu-<?php echo $i ?>">Tên vật tư</label>
            <input id="txt-ten-vat-tu-<?php echo $i ?>" value="<?php echo $ten_vat_tu ?>" name="ten_vat_tu[]"
                   type="text"
                   class="form-text text txt-ten-vat-tu <?php echo (isset($_POST['error']['ten_vat_tu']) && $_POST['error']['key'] == $key)
                     ? 'error' : '' ?>" autocomplete="OFF"/>
          </div>
          <div class="form-item">
            <input id="field-remove-submit-<?php echo $i ?>" type="button" value="X"
                   class="field-remove-submit form-submit"/>
          </div>
          <div class="form-item form-text-wrapper">
            <label for="txt-so-luong-<?php echo $i ?>">Số lượng</label>
            <input id="txt-so-luong-<?php echo $i ?>" value="<?php echo $_POST['so_luong'][$key] ?>" name="so_luong[]"
                   type="text"
                   class="form-text text txt-so-luong <?php echo (isset($_POST['error']['so_luong']) && $_POST['error']['key'] == $key)
                     ? 'error' : '' ?>" autocomplete="OFF"/>
            <input id="txt-don-vi-<?php echo $i ?>" value="<?php echo $_POST['don_vi'][$key] ?>" name="don_vi[]"
                   type="text"
                   class="form-text text txt-don-vi <?php echo (isset($_POST['error']['don_vi']) && $_POST['error']['key'] == $key)
                     ? 'error' : '' ?>" autocomplete="OFF"/>
          </div>
          <div class="form-item form-text-wrapper">
            <label for="txt-don-gia-<?php echo $i ?>">Đơn giá</label>
            <input id="txt-don-gia-<?php echo $i ?>" value="<?php echo $_POST['don_gia'][$key] ?>" name="don_gia[]"
                   type="text"
                   class="form-text text txt-don-gia <?php echo (isset($_POST['error']['don_gia']) && $_POST['error']['key'] == $key)
                     ? 'error' : '' ?>" autocomplete="OFF"/>
          </div>
          <div class="clear"></div>
        </div>
        <?php } ?>


      <?php } else if(isset($data['chi_tiet'])){ ?>


      <?php foreach ($data['chi_tiet'] as $key => $vat_tu) { ?>
        <?php
        $ten_vt = $vat_tu->ten_vat_tu;
      $don_vi = $vat_tu->don_vi;
      $so_luong = $vat_tu->so_luong;
        $don_gia = $vat_tu->don_gia;

        $i++;

        ?>
        <div class="form-item-row">
          <div class="form-item form-text-wrapper">
            <label for="txt-ten-vat-tu-<?php echo $i ?>">Tên vật tư</label>
            <input id="txt-ten-vat-tu-<?php echo $i ?>" value="<?php echo $ten_vt ?>" name="ten_vat_tu[]"
                   type="text"
                   class="form-text text txt-ten-vat-tu <?php echo (isset($_POST['error']['ten_vat_tu']) && $_POST['error']['key'] == $key)
                     ? 'error' : '' ?>" autocomplete="OFF"/>
          </div>
          <div class="form-item">
            <input id="field-remove-submit-<?php echo $i ?>" type="button" value="X"
                   class="field-remove-submit form-submit"/>
          </div>
          <div class="form-item form-text-wrapper">
            <label for="txt-so-luong-<?php echo $i ?>">Số lượng</label>
            <input id="txt-so-luong-<?php echo $i ?>" value="<?php echo $so_luong ?>" name="so_luong[]"
                   type="text"
                   class="form-text text txt-so-luong <?php echo (isset($_POST['error']['so_luong']) && $_POST['error']['key'] == $key)
                     ? 'error' : '' ?>" autocomplete="OFF"/>
            <input id="txt-don-vi-<?php echo $i ?>" value="<?php echo $don_vi ?>" name="don_vi[]"
                   type="text"
                   class="form-text text txt-don-vi <?php echo (isset($_POST['error']['don_vi']) && $_POST['error']['key'] == $key)
                     ? 'error' : '' ?>" autocomplete="OFF"/>
          </div>
          <div class="form-item form-text-wrapper">
            <label for="txt-don-gia-<?php echo $i ?>">Đơn giá</label>
            <input id="txt-don-gia-<?php echo $i ?>" value="<?php echo $don_gia ?>" name="don_gia[]"
                   type="text"
                   class="form-text text txt-don-gia <?php echo (isset($_POST['error']['don_gia']) && $_POST['error']['key'] == $key)
                     ? 'error' : '' ?>" autocomplete="OFF"/>
          </div>
          <div class="clear"></div>
        </div>
        <?php } ?>


      <?php } else { ?>
      <?php $i++; ?>
      <div class="form-item-row">
        <div class="form-item form-text-wrapper">
          <label for="txt-ten-vat-tu-1">Tên vật tư</label>
          <input id="txt-ten-vat-tu-1" name="ten_vat_tu[]" type="text"
                 class="form-text text txt-ten-vat-tu" autocomplete="OFF"/>
        </div>
        <div class="form-item">
          <input id="field-remove-submit-1" type="button" value="X" class="field-remove-submit form-submit"/>
        </div>
        <div class="form-item form-text-wrapper">
          <label for="txt-so-luong-1">Số lượng</label>
          <input id="txt-so-luong-1" name="so_luong[]" type="text" class="form-text text txt-so-luong" autocomplete="OFF"/>
          <input id="txt-don-vi-1" name="don_vi[]" type="text" class="form-text text txt-don-vi" autocomplete="OFF"/>
        </div>
        <div class="form-item form-text-wrapper">
          <label for="txt-don-gia-1">Đơn giá</label>
          <input id="txt-don-gia-1" name="don_gia[]" type="text" class="form-text text txt-don-gia" autocomplete="OFF"/>
        </div>
        <div class="clear"></div>
      </div>
      <?php } ?>
    </div>
    <div id="add-more-wrapper">
      <input id="add-more-vat-tu" type="button" value="Thêm vật tư" class="field-add-more-submit form-submit"/>
    </div>
  </fieldset>
  <div class="form-item form-textarea-wrapper">
    <label for="ghi-chu">Ghi chú</label>
    <textarea id="ghi-chu" name="ghi_chu"
              class="form-textarea <?php echo ($_POST && (string) $_POST['error']['key'] == 'ghi_chu') ? 'error'
                : '' ?>" cols="60" rows="5"><?php echo ($_POST) ? $_POST['ghi_chu'] : (($data ? $data['phieu']['ghi_chu'] : '')) ?></textarea>

    <div class="description">Nhập tối đa 500 ký tự. Số lượng hiện thời: <span style="color: red;"
                                                                              id="ghi-chu-length">0</span></div>
  </div>
  <div id="submit-wrapper">
    <input type="hidden" id="total-post" value="<?php echo $i  ?>" disabled="disabled"/>
    <input type="hidden" name="ma_phieu" value="<?php echo ($data) ? $data['phieu']['ma_phieu'] : ''  ?>"/>
    <input type="hidden" name="type" value="<?php echo ($data) ? $data['type'] : ''  ?>"/>
    <input id="submit" type="submit" onclick="return check_nhap();" value="Nhập dữ liệu" class="form-submit"/>
<!--    <input id="submit" type="submit" value="Nhập dữ liệu" class="form-submit" style="display: none;"/>-->
  </div>
</form>